package it.eng.area118.mdogpscollector;

import java.util.List;

import it.eng.area118.mdocommon.Callback;
import it.eng.area118.mdocommon.android.notification.AndroidNotificationManager;
import it.eng.area118.mdocommon.android.util.AndroidApplicationContext;
import it.eng.area118.mdocommon.android.util.StringUtils;
import it.eng.area118.mdocommon.notification.InputTextDialogCallback;
import it.eng.area118.mdogpscollector.activity.ManageCheckPointActivity;
import it.eng.area118.mdogpscollector.command.ListDistinctNameCommand;
import it.eng.area118.mdogpscollector.command.SearchFilter;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import it.eng.area118.mdogpscollector.service.CheckPointLookupService;
import it.eng.area118.mdogpscollector.util.ViewUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SearchBarFragment extends Fragment {

	private HomeCallback callback;
	private TextView km;
	private View kmContainer;
	private View picker;
	private View nameContainer;

	private TextView name;

	private boolean searchName;
	private Button searchBt;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.searchbar_vertical_layout, container, false);
		initSearchBt(rootView);

		Button insert = ViewUtils.getView(rootView, R.id.btInsertCheckPoint);
		if (insert != null) {
			insert.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (callback != null) {
						callback.onInsertNewPoint();
					}

				}
			});

		}
		initKmChoice(rootView);
		initPointNameChoice(rootView);
		// km = ViewUtils.getView(rootView, R.id.search_field_km);
		//
		// kmContainer = (View) ViewUtils.getView(rootView, R.id.search_field_km_container);
		// picker = rootView.findViewById(R.id.number_picker);
		// if (picker == null) {
		// picker =
		// it.eng.area118.mdocommon.android.util.ViewUtils.inflate(AndroidApplicationContext.getInstance().getApplicationContext(),
		// R.layout.number_picker_layout);
		// }
		// kmContainer.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// int initValue = 0;
		// try {
		// Integer.valueOf(km.getText().toString());
		// } catch (Exception e) {
		// initValue = 0;
		// }
		// int[] btExtra = new int[] { R.id.btPickerPlus, R.id.btPickerMinus };
		// AndroidNotificationManager.getInstance().showNumberPickerDialog("Kilometro",
		// "Inserire il km di ricerca(intervallo aperto)", picker, R.id.number_picker, R.layout.number_picker_layout,
		// btExtra, initValue, 50, new InputTextDialogCallback() {
		//
		// @Override
		// public void onDataReceived(String data) {
		// km.setText(data);
		// }
		// });
		// }
		// });
		return rootView;
	}

	private void initPointNameChoice(View rootview) {
		nameContainer = (View) ViewUtils.getView(rootview, R.id.search_field_name_container);
		name = ViewUtils.getView(rootview, R.id.search_field_checkPointDesc);
		if (nameContainer != null) {
			nameContainer.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ListDistinctNameCommand list = new ListDistinctNameCommand();
					Callback<List<String>> cb = new Callback<List<String>>() {

						private ProgressDialog dialog;

						@Override
						public void onBeforeExecute() {
							dialog = ProgressDialog.show(AndroidApplicationContext.getInstance().getApplicationContext(), "", "Caricamento in corso...", true, false);

						}

						@Override
						public void onSuccess(List<String> result) {
							if (dialog != null) {
								dialog.dismiss();
							}
							AndroidNotificationManager.getInstance().showOptionDialog("Scegliere Nome/Tipologia punto", "Scegliere una delle seguenti opzioni", result.toArray(new String[] {}), new InputTextDialogCallback() {

								@Override
								public void onDataReceived(String data) {
									if (name != null) {
										String[] splitted = data.split("-");
										name.setText(splitted[1]);
										searchName = true;
									}

								}
							});

						}

						@Override
						public void onFailure(Throwable e) {
							if (dialog != null) {
								dialog.dismiss();
							}
							AndroidNotificationManager.getInstance().showSimpleAlert("Attenzione!", "Database occupato riprovare per favore", null);
							CheckPointLookupService.reset();

						}

						@Override
						public void onCancelled() {
							if (dialog != null) {
								dialog.dismiss();
							}
							AndroidNotificationManager.getInstance().showSimpleAlert("Attenzione!", "Database occupato riprovare per favore", null);
							CheckPointLookupService.reset();

						}
					};
					list.execute(cb, null);

				}
			});
		}
	}

	private void initKmChoice(View rootview) {
		km = ViewUtils.getView(rootview, R.id.search_field_km);

		kmContainer = (View) ViewUtils.getView(rootview, R.id.search_field_km_container);

		picker = rootview.findViewById(R.id.number_picker);
		if (picker == null) {
			picker = it.eng.area118.mdocommon.android.util.ViewUtils.inflate(AndroidApplicationContext.getInstance().getApplicationContext(), R.layout.number_picker_layout);
		}
		if (kmContainer != null) {
			kmContainer.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int initValue = 0;
					try {
						Integer.valueOf(km.getText().toString());
					} catch (Exception e) {
						initValue = 0;
					}
					int[] btExtra = new int[] { R.id.btPickerPlus, R.id.btPickerMinus };
					AndroidNotificationManager.getInstance().showNumberPickerDialog("Kilometro", "Inserire il km di ricerca(intervallo aperto)", picker, R.id.number_picker, R.layout.number_picker_layout, btExtra, initValue, 50, new InputTextDialogCallback() {

						@Override
						public void onDataReceived(String data) {
							km.setText(data);
						}
					});
				}
			});
		}
	}

	private void initSearchBt(final View rootView) {
		searchBt = ViewUtils.getView(rootView, R.id.btSearchPoints);
		if (searchBt != null) {
			searchBt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					TextView name = (TextView) rootView.findViewById(R.id.search_field_checkPointDesc);

					final String sName = StringUtils.defaultIfEmpty(name.getText().toString(), "");
					String k = "";
					try {
						k = String.valueOf(Integer.parseInt("" + km.getText()));
					} catch (Exception e) {
						k = "";
					}
					String sKm = StringUtils.defaultIfEmpty(k, "");
					SearchFilter filter = new SearchFilter();
					filter.setName(sName);
					filter.setKm(sKm);
					filter.setOpenKmRange(true);
					callback.onSearch(filter);

				}
			});
		}
	}

	public static interface HomeCallback {
		public void onSaveCheckPoint(CheckPoint cp);

		public void onSearch(SearchFilter filter);

		public void onInsertNewPoint();
	}

	public void setOnSearchListener(HomeCallback callback) {
		this.callback = callback;
	}

}
